package com.woniuxy.stream;

import org.junit.Test;

import java.util.Optional;

/**
 * <P> @Author: ideaAdmin
 * <P> TODO: Class
 * <P> TODO:
 * <P> Date: 2022/4/1:19:33
 * <P> Description:
 */

public class OptionDemo {

    public static Optional<Author> getAuthor() {
        Author author = new Author(1L, "蒙多", 0, "一个从菜刀中明悟哲理的祖安人", null);
        //Author author = new Author(1L, "蒙多", 33, "一个从菜刀中明悟哲理的祖安人", null);

        return Optional.ofNullable(null);
    }

        public static void main(String[] args) {
        Optional<Author> author = getAuthor();
        author.get();
        author.ifPresent(author1 -> System.out.println(author1));
    }
    /**
     * TODO: orElseGet() 安全的获取值
     **/
    @Test
    public void o1() {
        Author author = getAuthor().orElseGet(() -> new Author());

    }

    /**
     * TODO: orElseThrow() 如果为空 抛出异常
     **/
    @Test
    public void o2() {
        getAuthor().orElseThrow(() -> new RuntimeException("数据为空"));
    }


    /**
     * TODO: filter() 过滤方法
     **/
    @Test
    public void o3() {
        Optional<Author> author1 = getAuthor().filter(author -> author.getName().length() > 2);
    }

    /**
     * TODO: filter() 过滤方法
     **/
    @Test
    public void o4() {
        Optional<Author> author = getAuthor();
        if(author.isPresent()){
            Author author1 = author.get();
            author1.getName();
            author1.getAge();
            System.out.println(author1.getAge());
        }
    }
}
